<?php

include "../database.php";
session_start();
//Izračunamo povprečje v dani tabeli števil
function average($array) {
    $sum = 0;
    $num = 0;
    foreach ($array as $id => $est) {
        if($est>0){
            $sum+=$est;
            $num++;
        }
    }

    return round(($num > 0) ? ($sum / $num) : 0, 2);   //na 2 decimalko tocno
}
//preverimo, če je uporabnik v določeni rundi oddal glas
function oddalGlas($round, $id, $story_id){
    $query = "SELECT * FROM planning_poker WHERE story_id = '$story_id' AND round = '$round' AND user_id = '$id'";
    if(mysql_num_rows(executeQuery($query))==0)
        return false;
    else return true;
}
function getUsers($project_id){
    //PREBIRANJE UPORABNIKOV
    $users_query = "SELECT DISTINCT id, username FROM user u, project_user p WHERE p.project_id = '$project_id' AND p.user_id = u.id OR u.id IN (SELECT scrum_master FROM project WHERE project_id='$project_id') ORDER BY u.id ASC";
    $result_users = executeQuery($users_query);
    $allUsers = array();
    while ($row = mysql_fetch_array($result_users)) {
        array_push($allUsers, $row);
    }
    return $allUsers;
}
//Zgradimo pripravljeno tabelo za izris
function build2Dtable($story_id, $project_id){
    
    $query = "SELECT * FROM planning_poker WHERE story_id = '$story_id' ORDER BY round ASC, user_id ASC";
    $result = executeQuery($query);
    //BUILDING ORGANIZED TABLE
    $whole_table = array();
    //READ whole SQL table
    while ($row = mysql_fetch_array($result)) {
        array_push($whole_table, $row);
    }
    $rounds = (count($whole_table) > 0) ? $whole_table[count($whole_table) - 1]['round'] : 0;

    //PREBIRANJE UPORABNIKOV
    $users_query = "SELECT id, username FROM user u, project_user p WHERE p.project_id = '$project_id' AND p.user_id = u.id ORDER BY u.id ASC";
    $result_users = executeQuery($users_query);
    $allUsers = getUsers($project_id);
    $ordered_table = array();
    foreach($allUsers as $user) {
        $u_id = $user['id'];
        for($i=1; $i<=$rounds; $i++){
            $ordered_table[$i][$u_id]="0";
        }
    }
    
    foreach($whole_table as $i=>$row){
        $ordered_table[$row['round']][$row['user_id']]=$row['estimate'];
    }

    //print_r($ordered_table);
    return $ordered_table;
}


$story_id = $_GET['story_id'];
$query = "SELECT project_id, poker_active FROM story WHERE story_id = '$story_id'";
$row = mysql_fetch_array(executeQuery($query));
$project_id = $row[0];
$poker_active = $row[1];
//Zgradimo 2D tabelo, pipravljeno za prikaz v HTML
$ordered_table = build2Dtable($story_id, $project_id);

$returnArray = array();
//IZPIS IMENA ZGODBE
$query = "SELECT story_title FROM story WHERE story_id = '$story_id'";
$title = mysql_fetch_array(executeQuery($query));
$table = "<h4>Story: $title[0]</h2><br />";
//Izris samo, če je vsaj ena vrstica, runda v tabeli
if (count($ordered_table) > 0 || $poker_active > 0) {
    
    //PRINTING TABLE HEAD
    $table .= "<table>
            <thead><td>round</td>";
    $allUsers = getUsers($project_id);
    foreach($allUsers as $u){
        $table.="<td>".$u['username']."</td>";
    }
    $table.="<td>average (pts)</td></thead>";
    //END OF HEAD, STARTING CONTENT
    $blendRow = false;
    $id = $_SESSION['id'];
    foreach ($ordered_table as $round => $estimates){
        //kdaj zakrijemo vsebino
        if($round == $poker_active && !oddalGlas($round, $id, $story_id)){
            $blendRow=true;
            break;
        }
        $act = ($round == $poker_active)?" class='poker_active_round'":"";
        $table.="<tr><td$act>$round</td>";
        
        //Izpis ocen posameznih uporabnikov
        foreach ($allUsers as $u){
            $est = ($estimates[$u['id']]==0)?"-":$estimates[$u['id']];  //Izpisemo - namesto 0
            $table.="<td>".$est."</td>";
        }
        $avg = average($estimates);
        $table.="<td$act>$avg</td></tr>";
    }
    //Izris zakrite vrstice
    if($blendRow || $poker_active>count($ordered_table)){
        $msg = "Submit your evaluation.";
        $table.="<tr class='current_round'><td>$poker_active</td><td colspan=".(count($allUsers)+1).">$msg</td></tr>";
        $blendRow=false;
    }
    //END OF CONTENT
    $table.="</table>";
    
    if ($poker_active > 0 && !oddalGlas($poker_active, $id, $story_id)) {
        $controls.="<input id='poker_time_estimate' type='text' name='poker_time_estimate' placeholder='your estimate'/>
                 <button id='poker_time_estimate_submit' type='submit'>submit</button>";
    }
    
} else {
    $table .= "No planning poker yet on this story.";
    $controls = "";
}
$returnArray["table"] = $table;
$returnArray["controls"] = $controls;
$returnArray["poker_active"] = $poker_active;
echo json_encode($returnArray);
?>
